package com.crl.module.dragger
{
	import com.crl.geom.IntPoint;
	import com.crl.module.dragger.events.DragEvent;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.Dictionary;
	
	public class AbstractDragger extends Sprite
	{
		private static const MIN_DISTANCE:int=3;
		
		private var _dropTrigger:Boolean;
		private var _dragEnabled:Boolean;
		
		
		private var dragAcceptableChecker:Function;
		
		public function AbstractDragger()
		{
			super();
			
			addEventListener(MouseEvent.MOUSE_DOWN, __mouseDown);
		}
		
		/**
		 * 是否为此容器接受的拖曳对像; 
		 * @param com
		 * @param state 哪个阶段
		 * @return 
		 * 
		 */		
		public function isDragAcceptableInitiator(com:AbstractDragger,state:int):Boolean{
			if(dragAcceptableChecker != null){
				return dragAcceptableChecker(com,state);
			}else{
				return false;
			}
		}
		
		/**
		 * 放入检查函数; 
		 * @param func
		 * 
		 */		
		public function setDragAcceptableHandler(func:Function):void{
			dragAcceptableChecker = func;
		}
		
		
		/**
		 * 设置是否接受拖曳; 
		 * @param b
		 * 
		 */		
		public function set dragEnabled(b:Boolean):void{
			_dragEnabled = b;
		}
		public function get dragEnabled():Boolean{
			return _dragEnabled;
		}
		
		/**
		 * 是否为拖拽目标;
		 * @param b
		 * 
		 */		
		public function set dropTrigger(b:Boolean):void{
			_dropTrigger = b;
		}
		public function get dropTrigger():Boolean{
			return _dropTrigger;
		}
		
		
		
		/**
		 * 取 
		 * @return 
		 * 
		 */		
		public function getMousePosition():Point{
			return new Point(mouseX, mouseY);
		}
		
		/**
		 * 取得全局坐标; 
		 * @param rv
		 * @return 
		 * 
		 */	    
		public function getGlobalLocation(rv:Point=null):Point{
			if(!rv){
				rv=new Point(0, 0);
			}
			return localToGlobal(rv);
		}
		
		private function __mouseDown(e:MouseEvent):void{			
			if(dragEnabled){
				addEventListener(MouseEvent.MOUSE_MOVE, __mouseMove);
				addEventListener(MouseEvent.ROLL_OUT, __rollOut);
				stage.addEventListener(MouseEvent.MOUSE_UP, __mouseUp, false, 0, true);
				pressingPoint = getMousePosition();
			}
		}
		
		private var pressingPoint:Point;
		private function __mouseUp(e:MouseEvent):void{
			stopListernDragRec();
		}
		private function __mouseMove(e:MouseEvent):void{
			var mp:Point = getMousePosition();
			if(Point.distance(mp,pressingPoint) > MIN_DISTANCE){
				fireDragRecognizedEvent();
				stopListernDragRec();
			}
		}
		private function __rollOut(e:MouseEvent):void{
			stopListernDragRec();
		}
		private function stopListernDragRec():void{
			removeEventListener(MouseEvent.MOUSE_MOVE, __mouseMove);
			removeEventListener(MouseEvent.ROLL_OUT, __rollOut);
			stage.removeEventListener(MouseEvent.MOUSE_UP, __mouseUp);
		}
		
		/**
		 * 触发用户想拖动事件; 
		 * @param touchedChild
		 * 
		 */		
		private function fireDragRecognizedEvent():void{
			dispatchEvent(new DragEvent(DragEvent.DRAG_RECOGNIZED, this, null));
		}
		
	}
}